package com.mlnx.push.websocket.server.topic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by amanda.shan on 2018/3/2.
 */
public class UserTopicManager {

    private Map<Integer, List<TopicType>> topicMap = new HashMap<Integer, List<TopicType>>();

    public void removeDevice(Integer userId) {
        topicMap.remove(userId);
    }

    public UserTopicManager lisHeart(Integer userId) {
        addTopic(userId, TopicType.U_ECG_HEART_TOPIC);
        return this;
    }

    public UserTopicManager lisRealAnaly(Integer userId) {
        addTopic(userId, TopicType.U_ECG_REAL_ANALY_TOPIC);
        return this;
    }

    public UserTopicManager lisEcgDevice(Integer userId) {
        addTopic(userId, TopicType.U_ECG_DEVICE_TOPIC);
        return this;
    }

    public UserTopicManager lisBp(Integer userId) {
        addTopic(userId, TopicType.U_BP_TOPIC);
        return this;
    }

    public UserTopicManager lisSpo(Integer userId) {
        addTopic(userId, TopicType.U_SPO_TOPIC);
        return this;
    }

    public UserTopicManager lisBt(Integer userId) {
        addTopic(userId, TopicType.U_BT_TOPIC);
        return this;
    }

    public UserTopicManager lisTemp(Integer userId) {
        addTopic(userId, TopicType.U_TEMP_TOPIC);
        return this;
    }

    public UserTopicManager lisCO2(Integer userId) {
        addTopic(userId, TopicType.U_CO2_TOPIC);
        return this;
    }

    public UserTopicManager lisExigence(Integer userId) {
        addTopic(userId, TopicType.U_DEVICE_EXIGENCE_TOPIC);
        return this;
    }

    public UserTopicManager lisBpElectric(Integer userId) {
        addTopic(userId, TopicType.U_BP_ELECTRIC_TOPIC);
        return this;
    }

    public UserTopicManager lisWatchElectric(Integer userId) {
        addTopic(userId, TopicType.U_WATCH_ELECTRIC_TOPIC);
        return this;
    }

    public UserTopicManager addTopic(Integer userId, TopicType topicType) {
        List<TopicType> topicTypes = topicMap.get(userId);
        if (topicTypes == null) {
            topicTypes = new ArrayList<TopicType>();
            topicMap.put(userId, topicTypes);
        }
        if (!topicTypes.contains(topicType)) {
            topicTypes.add(topicType);
        }

        return this;
    }

    public List<Topic> getTopics() {

        List<Topic> topics = new ArrayList<Topic>();

        for (Integer userId : topicMap.keySet()) {
            List<TopicType> topicTypes = topicMap.get(userId);
            for (TopicType topicType : topicTypes) {
                topics.add(new UserTopic(topicType, userId));
            }
        }

        return topics;
    }

    public static void main(String[] args) {
        UserTopicManager deviceTopicManager = new UserTopicManager();
//        deviceTopicManager.lisEcgDevice("cms0001").lisBp("cms0001");

        deviceTopicManager.lisSpo(44943).lisBp(44943);
        String msg = JSON.toJSONString(deviceTopicManager.getTopics());

        JSONArray jsonArray = JSON.parseArray(msg);
        System.out.println(jsonArray.get(0).toString().contains("userId"));
    }
}
